(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.addFeeForPayController", addFeeForPayController);
	addFeeForPayController.$inject = [ "$scope", "$q", "$uibModalInstance", "ajaxServiceFactory", "dialogsModule", "gridService", "modalService", "layoutConfig", "validatorConfig", "authService",
			"$filter", "arithService" ];
	function addFeeForPayController($scope, $q, $uibModalInstance, ajaxServiceFactory, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, $filter, arithService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("addFeeForPayLookup"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getFeeCodeOptions : {
				url : "do/cls/settlement/feeCodeOptions",
				method : "post"
			},
			getFeeCcyOptions : {
				url : "do/cls/dataList/currencies",
				method : "get"
			},
			getAllocCodeOptions : {
				url : "do/cls/dataList/allocCodeList",
				method : "get"
			}
		});

		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.settlement = ctrl.settlement || {};
		ctrl.ok = function() {
			var feeObj = ctrl.settlement.feeObj;
			var feeCode = feeObj.feeCode;
			var feeAmt = feeObj.feeAmt;
			var feeCcy = feeObj.feeCcy;
			if (feeCode != "" && (feeAmt - 0) != 0 && feeCcy != "") {
				var param = new Object();
				param.itemType = "F";
				param.crdr = feeObj.crDrFlag;
				param.itemCcy = feeCcy;
				param.taxAmt = feeObj.feeTaxAmt ? feeObj.feeTaxAmt : 0;
				param.itemAmt = arithService.calc(feeAmt, param.taxAmt, "+");
				param.payCcy = feeCcy;
				param.payAmt = arithService.calc(feeAmt, param.taxAmt, "+");
				param.allocCode = feeObj.allocCode;
				param.itemCode = feeCode;
				param.itemDesc = feeObj.feeDesc;
				param.itemSubtype = "";

				param.fxType = "C";
				param.fxRate = 1.0;
				param.fxType2 = "C";
				param.fxRate2 = 1.0;

				param.holdFlag = "N";
				param.waiveFlag = "N";
				param.uItemWaive = "N";
				param.carryFlag = "N";
				param.transferFlag = "N";

				param.taxWaiveFlag = "N";
				param.childFlag = "N";
				param.feeNewFlag = "Y";

				param.feeId = "";
				param.feeTaxFlag = feeObj.feeTaxFlag;

				param.parentItemSeq = 0;

				if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](param) !== false) {
					$uibModalInstance.close();
				}
			} else {
				if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"]() !== false) {
					$uibModalInstance.close();
				}
			}

		};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedTxnFeeList = selectedRows;
		}

		ctrl.getFeeCodeOptions = function(feeObj) {
			var defer = $q.defer();
			ajaxService.getFeeCodeOptions(feeObj).then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		};

		ctrl.getFeeCcyOptions = function() {
			var defer = $q.defer();
			ajaxService.getFeeCcyOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};

		ctrl.getAllocCodeOptions = function() {
			var defer = $q.defer();
			ajaxService.getAllocCodeOptions().then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		};

		ctrl.isShowFeeObj = function() {
			if (!ctrl.settlement.txnFees || ctrl.settlement.txnFees.length == 0) {
				return false;
			} else {
				return true;
			}
		};

		ctrl.addPaidFee = function() {
			if (!ctrl.selectedTxnFeeList || ctrl.selectedTxnFeeList.length == 0) {
				modalService.openErrorDialogs({
					messages : [ "title.dialogs.info.norecsel" ]
				});
				return;
			}
			var txnFee = selectedTxnFeeList[0];
			var param = new Object();
			param.itemType = "F";
			param.crdr = "C";
			param.itemCcy = trim(txnFee.itemCcy);
			param.itemAmt = trim(txnFee.itemAmt) * 1;
			param.payCcy = trim(txnFee.paidCcy);
			param.payAmt = trim(txnFee.paidAmt) * 1;
			param.fxType = "C";
			param.fxRate = trim(txnFee.pcfxRate) * 1;
			param.fxType2 = "C";
			param.fxRate2 = trim(txnFee.fcfxRate2) * 1;
			param.itemCode = trim(txnFee.itemCode);
			param.itemDesc = trim(txnFee.itemDesc);
			param.feeTaxFlag = trim(txnFee.taxFlag);
			param.taxAmt = trim(txnFee.taxAmt);
			param.taxWaiveFlag = trim(txnFee.taxWaiveFlag);
			param.allocCode = trim(txnFee.allocCode);
			param.feeId = trim(txnFee.feeId);

			param.itemSubtype = "";
			param.holdFlag = "N";
			param.waiveFlag = "N";
			param.uItemWaive = "N";
			param.carryFlag = "N";
			param.transferFlag = "N";

			param.childFlag = "N";
			param.feeNewFlag = "Y";

			param.parentItemSeq = "0";
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](param) !== false) {
				$uibModalInstance.close();
			}
		};

		ctrl.feeCodeChanged = function() {
			if (ctrl.settlement && ctrl.settlement.feeObj && ctrl.settlement.feeObj.feeCode) {
				if ($filter("encode")(ctrl.settlement.feeObj.feeCode_old) == $filter("encode")(ctrl.settlement.feeObj.feeCode)) {
					return;
				} else {
					ctrl.settlement.feeObj.feeCode_old = ctrl.settlement.feeObj.feeCode;
				}
				if(ctrl.settlement.feeObj && !ctrl.settlement.feeObj.loanId){
					delete ctrl.settlement.feeObj.loanId;
				}
				authService.lookup(ctrl.settlement).then(function(response) {
					angular.merge(ctrl.settlement, response.data);
					setFeeCodeValue(ctrl.feeCodeOptions, ctrl.settlement.feeObj);
					ctrl.ccy = null;
					if (ctrl.settlement && ctrl.settlement.feeObj) {
						ctrl.ccy = ctrl.settlement.feeObj.feeCcy;
					}
					if (ctrl.ccy == null) {
						ctrl.ccy = "";
					}
				}, angular.noop);
			}
		};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.txnFees.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = "vm.settlement.txnFees";
			if (ctrl.dialogsModule.params.feeObj) {
				ctrl.settlement.feeObj = ctrl.dialogsModule.params.feeObj;
			}
			ctrl.getFeeCcyOptions().then(function(response) {
				ctrl.feeCcyOptions = response ? response.data : [];
			}, angular.noop);
			ctrl.getAllocCodeOptions().then(function(response) {
				ctrl.allocCodeOptions = response;
			}, angular.noop);
			ctrl.getFeeCodeOptions(ctrl.settlement.feeObj).then(function(response) {
				ctrl.feeCodeOptions = response ? response : [];
			}, angular.noop);

		};

		function setFeeCodeValue(options, feeObj) {
			for (var i = 0; i < options.length; i++) {
				if ($filter("encode")(options[i].value) == $filter("encode")(feeObj.feeCode)) {
					feeObj.feeCode = options[i].value;
					break;
				}
			}
		}
	}
})(angular);
